﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PJOnimal
{
    public class Sprite
    {
        public int time;
        public int tocDoAnimation = 100 ; //tốc độ animation (frame per second);
        public float phamvidichuyen = 0;
        private int _iType = 0;
        private float _height = 0;
        
        public float Height
        {
            get { return _height; }
            set { _height = value; }
        }
        private float _width = 0;

        public float Width
        {
            get { return _width; }
            set { _width = value; }
        }
        private float _dWidthL = 0;

        public float DWidthL
        {
            get { return _dWidthL; }
            set { _dWidthL = value; }
        }


        private float _dWidthR = 0;

        public float DWidthR
        {
            get { return _dWidthR; }
            set { _dWidthR = value; }
        }

        
        private float _dHeightO = 0;

        public float DHeightO
        {
            get { return _dHeightO; }
            set { _dHeightO = value; }
        }
        private float _dHeightU = 0;

        public float DHeightU
        {
            get { return _dHeightU; }
            set { _dHeightU = value; }
        }

       
        private int _flagMoveEnemy = 0;

        public int FlagMoveEnemy
        {
            get { return _flagMoveEnemy; }
            set { _flagMoveEnemy = value; }
        }
        public int IType
        {
            get { return _iType; }
            set { _iType = value; }
        }
        private float _x;
        public float X
        {
            get { return _x; }
            set { _x = value; }
        }
        private float _y;

        public float Y
        {
            get { return _y; }
            set { _y = value; }
        }
        private int _nTexture2d;

        public int NTexture2d
        {
            get { return _nTexture2d; }
            set { _nTexture2d = value; }
        }
        private Texture2D[] _texture2d;

        public Texture2D[] Texture2d
        {
            get { return _texture2d; }
            set
            {
                _texture2d = value;
                _nTexture2d = _texture2d.Length;
            }
        }
        private int _ispeed;

        public int Ispeed
        {
            get { return _ispeed; }
            set { _ispeed = value; }
        }
        private int _flagAction;

        private int _iText;

        public int IText
        {
            get { return _iText; }
            set { _iText = value; }
        }
        public int FlagAction
        {
            get { return _flagAction; }
            set { _flagAction = value; }
        }
        public Sprite()
        {
            _x = 0;
            _y = 0;
            _texture2d = null;
            _nTexture2d = 0;
            _flagAction = 0;
            _ispeed = 0;
            _iText = 0;
            _iType = 0;
        }
        public Sprite(float x, float y, Texture2D[] texture2d, int ntexture2d, int flag, int speed)
        {
            _x = x;
            _y = y;
            _texture2d = texture2d;
            _nTexture2d = ntexture2d;
            _flagAction = flag;
            _ispeed = speed;
            _iText = 0;
            _iType = 0;
        }
        public Sprite( Texture2D[] texture2d,int ntext, Vector2 xy ,int iType)
        {
            _x = xy.X;
            _y = xy.Y;
            _texture2d = texture2d;
            _nTexture2d = ntext;
            _flagAction = 0;
            _ispeed = 0;
            _iText = 0;
            _iType = iType;
        }
        public void UpdateDiChuyen(float x,float y)
        {
            _ispeed += 1;
            if (_ispeed < 10)
                return;
            else if (_flagAction != 11)
            {
                //_ispeed = 0;
                _x += x;
                _y += y;
            }
            else
                _y += y;
        }
        public void UpdateToaDo(float x, float y)
        {           
            _x = x;
            _y = y;
        }
        public void UpdateTancongEnemy(float x) 
        {
            if (x > _x)
                UpdateDiChuyen(4,0);
            else
                UpdateDiChuyen(-4, 0);
        }
        public void UpdateDuongDi()
        {
            float TocDo = 4;
            phamvidichuyen += TocDo;
            if (Math.Abs(phamvidichuyen) > 300 || Math.Abs(phamvidichuyen) < 0)
            {
                phamvidichuyen = -phamvidichuyen;  
                
            }
            if (phamvidichuyen < 0 )
            {
                int kk = IType;
                UpdateFlagAction(1);
                TocDo = -TocDo;
            }
            else
            {
                int kk = IType;
                UpdateFlagAction(5);
            }
            switch (IType)
            {
                case 30:
                    if ( _flagAction != 11)
                        _flagAction = 0;
                    break;
                case 40:
                    UpdateDiChuyen(TocDo,0);
                    break;
                case 50:
                    UpdateDiChuyen(TocDo, 0);
                    break;
                case 60:
                    UpdateDiChuyen(TocDo, 0);
                    break;                
            }
        }
        public void UpdateAnimationEnemy(GameTime gameTime,float x)
        {
            int nAnimation = 0;
            int nDie = 0;
           
            //time += gameTime.ElapsedGameTime.Milliseconds;
            time += 1;
            if (time > 10)
            {
                UpdateDuongDi();
                switch (IType)
                {
                    case 30:
                        nAnimation = 4;
                        nDie = 8;
                        break;
                    case 40:
                        nAnimation = 7;
                        nDie = 7;
                        break;
                    case 50:
                        nAnimation = 4;
                        nDie = 4;
                        break;
                    case 60:
                        nAnimation = 1;
                        nDie = 3;
                        break;
                    default :
                        nAnimation = 1;
                        nDie = 0;
                        break;
                }
                time = 0;
                
                switch (FlagAction)
                {
                    case 11:
                        _iText = nDie;
                        break;
                    case 100:
                        UpdateAnimation(gameTime);
                        break;
                    case 110:
                        UpdateAnimation(gameTime);
                        break;
                    default:
                        UpdateAnimation(gameTime);
                        break;   
                }
                //if (FlagAction == 11)
                //    _iText = 8;
                //else
                //    _iText = (_iText + 1) % 4;
                    
            }
        }
        public void UpdateFlagAction(int flag)
        {
            if (_flagAction != 11)
                _flagAction = flag;
        }
        public void UpdateAnimation(GameTime gameTime)
        {
            time += gameTime.ElapsedGameTime.Milliseconds;
            int nleft=0;
            int nright = 0;
            int nAnimation = 0;
            switch (IType)
            {
                case 30:
                    UpdateFlagAction(5);
                    nAnimation = 4;
                    nleft = 0;
                    nright = 4;
                    break;
                case 40:
                    UpdateFlagAction(5);
                    nAnimation = 7;
                    nleft = 0;
                    nright = 7;
                    break;
                case 50:
                    UpdateFlagAction(5);
                    nAnimation = 4;
                    nleft = 0;
                    nright = 4;
                    break;
                case 60:
                    UpdateFlagAction(5);
                    nAnimation = 1;
                    nleft = 0;
                    nright = 10;
                    break;
                default:
                    nAnimation = 7;
                    nleft = 0;
                    nright = 8;
                    break;
            }
            time = 101;           
            if (time > tocDoAnimation)
            {
                time = 0;
                switch (_flagAction)
                {
                    case 0:
                        _iText = 0;
                        break;
                    case 1:
                        _iText = (_iText + 1) % nAnimation + nleft;
                        break;
                    case 2:
                        _iText = 2;
                        break;
                    case 4:
                        _iText = 8;
                        break;
                    case 5:
                        _iText = ((_iText + 1) % nAnimation) + nright;
                        break;
                    case 6:
                        _iText = 10;
                        break;
                    case 10:
                        _iText = (_iText + 1) % 4;
                        break;
                }
            }
        }
        public void UpdateAnimationBacground(GameTime gameTime)
        {
            time += gameTime.ElapsedGameTime.Milliseconds;
            if (time > 1000)
            {
                time = 0;
                _iText = _flagAction;                
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
           //for (int i = 0; i < _nTexture2d;i++ )
                spriteBatch.Draw(_texture2d[_iText], new Vector2(_x, _y), Color.White);
        }
        public void DrawBackGround(SpriteBatch spriteBatch)
        {
            //for (int i = 0; i < _nTexture2d;i++ )
            spriteBatch.Draw(_texture2d[_iText], new Vector2(_x, _y), Color.White);
        }
        public void UpdateEnemy(float khoangCach)
        {
            _x = _x + khoangCach;
            _flagAction = 10;
        }
    }
}
